const JwtStrategy = require('passport-jwt').Strategy
const ExtractJwt = require('passport-jwt').ExtractJwt
const opts = {}
const config = require('./../config')
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken()
opts.secretOrKey = config.secretOrKey

module.exports = (passport) => {
  passport.use(
    new JwtStrategy(opts, async function (jwtPayload, done) {
      try {
        const { user_id } = jwtPayload
        if (user_id) {
          // 获取请求地址
          return done(null, jwtPayload)
        } else {
          return false
        }
      } catch (err) {
        console.log(err);
        return false
      }
    }),
  )
}
